using Core.SimpleTemp.Application;
using Core.SimpleTemp.Common.Authorization;
using Core.SimpleTemp.Common.PagingQuery;
using Core.SimpleTemp.Mvc.Api.Internal;
using Core.Toolkit.Authorization;
using Core.Toolkit.Core;
using Core.Toolkit.Core.Api;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Threading.Tasks;
using System.Linq;

namespace Core.SimpleTemp.Api.Api
{
    /// <summary>
    /// JG_JobsAssess
    ///</summary>
    [ApiExplorerSettings(GroupName = "Sys")]//swagger分类标签
    [Route("api/[controller]")]//路由说明
    [EnableCors("any")]//跨域规则
    [ApiController]//APi标识Controller
    [Authorize]//验证是否登录
    public class JG_JobsAssessController : CoreApiController
    {
        private readonly JG_JobsAssessService _service;
        private readonly JG_JobsAssessDetailService _serviceD;
        /// <summary>
        /// JG_JobsAssess
        ///</summary>
        public JG_JobsAssessController(JG_JobsAssessService service, JG_JobsAssessDetailService serviceD)
        {
            _service = service;
            _serviceD = serviceD;
        }

        #region 基础增删改查
        /// <summary>
        /// 查询
        ///</summary>
        /// <param name="pagingQueryModel"></param>
        /// <returns></returns>

        [HttpPost("GetList")]
        [PermissionFilter(nameof(JG_JobsAssessController) + "_" + nameof(JG_JobsAssessController.GetList), AuthorizationConst.LIST)]
        public async Task<IActionResult>
            GetList(PagingModel<JG_JobsAssessDto>
                pagingQueryModel)
        {
            IPageModel<JG_JobsAssessDto>
                result;
            result = await _service.GetList(pagingQueryModel);
            return this.JsonSuccess(result);
        }
        [HttpPost("GetUserList")]
        [PermissionFilter(nameof(JG_JobsAssessController) + "_" + nameof(JG_JobsAssessController.GetUserList), AuthorizationConst.LIST)]
        public async Task<IActionResult>
            GetUserList(PagingModel<JG_JobsAssessDto>
                pagingQueryModel)
        {
            IPageModel<JG_JobsAssessDto>
                result;
            result = await _service.GetUserList(pagingQueryModel);
            return this.JsonSuccess(result);
        }

        /// <summary>
        /// 添加/修改
        ///</summary>
        /// <param name="dto"></param>
        /// <returns></returns>

        [HttpPost("Save")]
        [PermissionFilter(nameof(JG_JobsAssessController) + "_" + nameof(JG_JobsAssessController.SaveAsync), AuthorizationConst.SAVE)]
        public async Task<IActionResult>
            SaveAsync(JG_JobsAssessDto dto)
        {
            dto.writeTime = DateTime.Now;
            //dto.CreateDept = dto.PostID;
            dto.CreateFactory = UserIdentity.Factory;
            //添加
            if (Equals(dto.Id, Guid.Empty))
            {
                var dto1=await _service.InsertAsync(dto);
                return this.JsonSuccess(dto1.Id);
            }
            else
            {
                var list = dto.JG_JobsAssessDetail.ToList();
               
                dto.JG_JobsAssessDetail.Clear();
                //await _service.Update(dto);
                await _service.UpdateAsync(dto);
                for (int i = 0; i < list.Count; i++)
                {
                    list[i].JGJobsAssessID = dto.Id;
                    await _serviceD.UpdateAsync(list[i]);
                }
                return this.JsonSuccess(string.Empty);
            }
        }
        /// <summary>
        /// 审核
        ///</summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost("Save2")]
        [PermissionFilter(nameof(JG_JobsAssessController) + "_" + nameof(JG_JobsAssessController.Save2Async), AuthorizationConst.SAVE)]
        public async Task<IActionResult>
            Save2Async(JG_JobsAssessDto dto)
        {

            dto.superiorLeaders= UserIdentity.Id;
            dto.approvalTime= DateTime.Now;
            dto.writeTime = DateTime.Now;
            dto.CreateFactory = UserIdentity.Factory;
            //dto.CreateDept = dto.PostID;
            //添加
            if (Equals(dto.Id, Guid.Empty))
            {
                await _service.InsertAsync(dto);
                return this.JsonSuccess(string.Empty);
            }
            else
            {
                var list = dto.JG_JobsAssessDetail.ToList();

                dto.JG_JobsAssessDetail.Clear();
                //await _service.Update(dto);
                await _service.UpdateAsync(dto);
                for (int i = 0; i < list.Count; i++)
                {
                    list[i].JGJobsAssessID = dto.Id;
                    if (list[i].CreateTime == null || Equals(list[i].Id, Guid.Empty))
                    {
                        await _serviceD.InsertAsync(list[i]);
                    }
                    else
                    {
                        await _serviceD.UpdateAsync(list[i]);
                    }
                    //try
                    //{
                    //    if (Equals(list[i].Id, Guid.Empty)) {
                    //        await _serviceD.InsertAsync(list[i]);
                    //    }
                    //    else
                    //    {
                    //        await _serviceD.UpdateAsync(list[i]);
                    //    }
                    //}
                    //catch (Exception ex)
                    //{

                        
                    //}
                }
                return this.JsonSuccess(string.Empty);
            }
        }
        /// <summary>
        /// DeleteMuti
        ///</summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpPost("DeleteMuti")]
        [PermissionFilter(nameof(JG_JobsAssessController) + "_" + nameof(JG_JobsAssessController.DeleteMutiAsync), AuthorizationConst.DELETE)]
        public async Task<IActionResult>
            DeleteMutiAsync([FromBody]Guid[] ids)
        {

            await _service.DeleteBatchAsync(ids);
            return this.JsonSuccess();
        }


        /// <summary>
        /// 根据ID获取信息
        ///</summary>
        /// <param name="id"></param>
        /// <returns></returns>

        [HttpGet("Get")]
        [PermissionFilter(nameof(JG_JobsAssessController) + "_" + nameof(JG_JobsAssessController.Get), AuthorizationConst.GET)]
        public async Task<IActionResult>
            Get(Guid id)
        {
            var model = await _service.GetAsync(id);
            if (model == null) return this.JsonFaild("未找到对应数据:" + id);
            return this.JsonSuccess(model);
        }


        /// <summary>
        /// 导出
        ///</summary>
        /// <param name="pagingQueryModel"></param>
        /// <returns></returns>

        [HttpPost("Export")]
        [PermissionFilter(nameof(JG_JobsAssessController) + "_" + nameof(JG_JobsAssessController.Export), AuthorizationConst.EXPORT)]
        public async Task<IActionResult>
            Export(PagingModel<JG_JobsAssessDto>
                pagingQueryModel)
        {
            var fileName = await _service.Export(pagingQueryModel);
            if (string.IsNullOrWhiteSpace(fileName))
                return this.JsonFaild(NULL_DATA_FOR_EXCEL_EXPORT);
            return this.JsonSuccess(fileName);
        }



        /// <summary>
        /// 查询部门用户
        ///</summary>
        /// 
        /// <returns></returns>

        [HttpPost("GetDepartmentUserList")]
        [PermissionFilter(nameof(JG_JobsAssessController) + "_" + nameof(JG_JobsAssessController.GetDepartmentUserList), AuthorizationConst.LIST)]
        public async Task<IActionResult> GetDepartmentUserList()
        {
            var result = (await _service.GetDepartmentUserList());

            return this.JsonSuccess(result);
        }
        #endregion

        /// <summary>
        /// 查询职务用户
        ///</summary>
        /// 
        /// <returns></returns>
        [HttpGet("GetDutyUser")]
        [HttpPost("GetDutyUser")]
        [PermissionFilter(nameof(JG_JobsAssessController) + "_" + nameof(JG_JobsAssessController.GetDutyUser), AuthorizationConst.LIST)]
        public async Task<IActionResult> GetDutyUser(String dutyID)
        {
            var result = (await _service.GetDutyUser(dutyID));

            return this.JsonSuccess(result);
        }


        /// <summary>
        /// 查询考核是否存在
        ///</summary>
        [HttpGet("getModel")]
        [HttpPost("getModel")]
        [PermissionFilter(nameof(JG_JobsAssessController) + "_" + nameof(JG_JobsAssessController.getModel), AuthorizationConst.LIST)]
        public async Task<IActionResult> getModel(PagingModel<JG_JobsAssessDto> pagingQueryModel)
        {
            IPageModel<JG_JobsAssessDto> result = await _service.GetList(pagingQueryModel);
            result = await _service.GetList(pagingQueryModel);
            return this.JsonSuccess(result);

        }
        /// <summary>
        /// 根据职务查询部门ID
        ///</summary>
        /// 
        /// <returns></returns>
        [HttpGet("GetDepartmentId")]
        [HttpPost("GetDepartmentId")]
        [PermissionFilter(nameof(JG_JobsAssessController) + "_" + nameof(JG_JobsAssessController.GetDepartmentId), AuthorizationConst.LIST)]
        public async Task<IActionResult> GetDepartmentId(String dutyID)
        {
            var result = (await _service.GetDepartmentId(dutyID));

            return this.JsonSuccess(result);
        }

    }
}